package com.example.test.util;


import com.example.test.entity.User;
//import com.example.test.mapper.ReposMapper;
//import com.example.test.mapper.RoleMapper;
//import com.example.test.mapper.RoleReposMapper;
import com.example.test.mapper.RoleMapper;
import com.example.test.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Slf4j
public class MyBatisUtil {

    private InputStream resource;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    public MyBatisUtil() throws IOException {
        resource = Resources.getResourceAsStream("SqlMapperConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        sqlSession = sqlSessionFactory.openSession();
    }

    public UserMapper userMapperLoad() throws IOException {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper;
    }

    public Integer getUserLength() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> all = mapper.findAll();
        Integer size = all.size();
        return size;
    }

    public void closeSqlSession() {
        sqlSession.close();
    }

    /**
     * 高级的UserMapper适配器，返回值为UserMapper对象，直接进行打印得出效果
     *
     * @param methodName
     * @param user
     * @param autoClose
     * @return
     */
    public UserMapper userMapperLoad(String methodName, User user, boolean autoClose) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        if ("insert".equals(methodName) && autoClose == true) {
            mapper.insert(user);
            sqlSession.commit();
        } else if ("delete".equals(methodName) && autoClose == true) {
            mapper.delete(user.getId());
            sqlSession.commit();
        } else if ("update".equals(methodName) && autoClose == true) {
            mapper.update(user);
            sqlSession.commit();
        } else {
            log.error("未查找到方法");
        }
        return mapper;
    }


    /**
     * 适配多数
     * 高级的UserMapper适配器，返回值为UserMapper对象，直接进行打印得出效果（重构）
     *
     * @param methodName
     * @param user
     * @param autoClose
     * @return
     */
    public UserMapper userMapperLoad(String methodName, User user, Integer id, boolean autoClose) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        if ("insert".equals(methodName) && autoClose == true) {
            mapper.insert(user);
            sqlSession.commit();
        } else if ("delete".equals(methodName) && autoClose == true) {
            mapper.delete(id);
            sqlSession.commit();
        } else if ("update".equals(methodName) && autoClose == true) {
            mapper.update(user);
            sqlSession.commit();
        } else {
            log.error("未查找到方法");
        }
        return mapper;
    }

    /**
     * 专为int/integer类型设计，无法进行插入
     * 高级的UserMapper适配器，返回值为UserMapper对象，直接进行打印得出效果（重构）
     *
     * @param methodName
     * @param id
     * @param autoClose
     * @return
     */
    public UserMapper userMapperLoad(String methodName, Integer id, boolean autoClose) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        if ("delete".equals(methodName) && autoClose == true) {
            mapper.delete(id);
            sqlSession.commit();
        } else {
            log.error("未查找到方法");
        }
        return mapper;
    }





    /**
     * 专为select设计进行查询所有
     *
     * @return
     * @throws IOException
     */
    public RoleMapper roleMapperLoad() throws IOException {
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        return mapper;
    }


//    public ReposMapper reposMapperLoad() {
//        ReposMapper mapper = sqlSession.getMapper(ReposMapper.class);
//        return mapper;
//    }
//
//    public RoleReposMapper roleReposMapperLoad() {
//        RoleReposMapper mapper = sqlSession.getMapper(RoleReposMapper.class);
//        return mapper;
//    }
}
